using System;
using System.Collections.Generic;
namespace SystemX.Infrastructure
{
    /// <summary>
    /// A generic collection with the ability to 
    /// check if it has been changed.
    /// </summary>
    [Serializable]
    public class StateList<T> : List<T>
    {

        #region Base overrides
        /// <summary>
        /// Serves as a hash function for a particular type. <see cref="M:System.Object.GetHashCode"></see> is suitable for use in hashing algorithms and data structures like a hash table.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"></see>.
        /// </returns>
        public override int GetHashCode()
        {
            string hash = string.Empty;
            foreach (T item in this)
            {
                hash += item.GetHashCode().ToString();
            }

            return hash.GetHashCode();
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"></see> is equal to the current <see cref="T:System.Object"></see>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"></see> to compare with the current <see cref="T:System.Object"></see>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"></see> is equal to the current <see cref="T:System.Object"></see>; otherwise, false.
        /// </returns>
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }

            if (obj.GetType() == this.GetType())
            {
                return obj.GetHashCode() == this.GetHashCode();
            }

            return false;
        }

        #endregion

        private int _HasCode = 0;

        /// <summary>
        /// Gets if this object's data has been changed.
        /// </summary>
        /// <returns>A value indicating if this object's data has been changed.</returns>
        public virtual bool IsChanged
        {
            get
            {
                return this.GetHashCode() != _HasCode;
            }
        }

        /// <summary>
        /// Marks the object as being clean, 
        /// which means not changed.
        /// </summary>
        public virtual void MarkOld()
        {
            _HasCode = this.GetHashCode();
            base.TrimExcess();
        }

    }
}